(function($){
	//右固定相关样式设置
	function setCss(jq){
		var datagridPanel = $(jq).parents(".panel.datagrid");
		datagridPanel.find(".datagrid-view1").css({
			left: 'auto',
			right: 0
		});
		datagridPanel.find(".datagrid-view2").css({
			left: 0,
			right: 'auto'
		});
		var width_view1 = datagridPanel.find(".datagrid-view1").width() + 18;
		var width_view2 = datagridPanel.find(".datagrid-view2").width() - 18;
		
		datagridPanel.find(".datagrid-view1").width(width_view1);
		datagridPanel.find(".datagrid-view1 .datagrid-header").width(width_view1);
		datagridPanel.find(".datagrid-view1 .datagrid-body").css({
			'overflow-y': 'auto',
			'padding-right': '18px',
			'width': width_view1 + 'px'
		});
		
		datagridPanel.find(".datagrid-view2").width(width_view2);
		datagridPanel.find(".datagrid-view2 .datagrid-header").width(width_view2);
		datagridPanel.find(".datagrid-view2 .datagrid-body").css({
			'padding-right': 0,
			'overflow-y': 'hidden',
			'overflow-x': 'auto',
			'width': width_view2 + 'px'
		});
		var target = datagridPanel.find(".datagrid-view2 .datagrid-body")[0];
		//变量windowWidth是可视区的宽度
		var windowWidth = target.clientWidth;
		//变量scrollWidth是滚动条的总宽度
		var scrollWidth = target.scrollWidth;
		if(scrollWidth > windowWidth){
			datagridPanel.find(".datagrid-view1 .datagrid-body-inner").css("padding-bottom","18px");
		} else {
			datagridPanel.find(".datagrid-view1 .datagrid-body-inner").css("padding-bottom","0");
		}
	}
	
	function addEvent(target1,target2,boo,target1_header,target2_header,timer1,timer2){
		target1.scroll(function () {
			if(timer1 != null) clearTimeout(timer1);
		    // 解除滚动监听事件，否则两个滚动条一起监听，会导致互相影响
		    target2.unbind("scroll mousewheel touchmove");
		    // 禁止另外一个滚动条滚动，只有在下面定时器绑定事件后才允许滚动
		    target2.bind('scroll mousewheel touchmove', function (e) {
		        e.preventDefault();
		        e.stopPropagation();
		        return false;
		    });
			var left = target1.scrollLeft(),
				top = target1.scrollTop();
			target1_header.scrollLeft(left);
		    target2.scrollTop(top);
			
		    timer1 = setTimeout(function () {
		        target2.unbind('scroll mousewheel touchmove');
		        addEvent(target2,target1,boo,target2_header,target1_header,timer2,timer1);
		    }, 500);
		});
		target1.bind("mousewheel",function (e){
			var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) ||  // chrome & ie
				(e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1));              // firefox
			var target = target1[0];
			//变量scrollTop是滚动条滚动时，距离顶部的距离
			var scrollTop = target.scrollTop + (-delta * 100);
			//变量windowHeight是可视区的高度
			var windowHeight = target.clientHeight;
			//变量scrollHeight是滚动条的总高度
			var scrollHeight = target.scrollHeight;
			
			//滚动条到底部的条件
			if(scrollTop + windowHeight >= scrollHeight){
				scrollTop = scrollHeight - windowHeight;
			} else if(scrollTop < 0) {
				scrollTop = 0;
			}
			
			target1.scrollTop(scrollTop);
			// target2.scrollTop(scrollTop);
		});
	}
	
	//右固定相关相关滚动事件处理
	function setScroll(jq){
		var datagridPanel = $(jq).parents(".panel.datagrid"),
			view1_body = datagridPanel.find(".datagrid-view1 .datagrid-body"),
			view2_body = datagridPanel.find(".datagrid-view2 .datagrid-body"),
			view1_header = datagridPanel.find(".datagrid-view1 .datagrid-header"),
			view2_header = datagridPanel.find(".datagrid-view2 .datagrid-header"),
			timer1 = null,timer2 = null;
		addEvent(view1_body,view2_body,$(jq).data('treegrid'),view1_header,view2_header,timer1,timer2);
		addEvent(view2_body,view1_body,$(jq).data('treegrid'),view2_header,view1_header,timer2,timer1);
	}
	var methods = {
		/**
		 * 有固定列
		 */
		rightFixed : function(jq) {
			return jq.each(function(){
				setCss(this);
				setScroll(this);
			});
		}
	};
	$.extend($.fn.datagrid.methods, methods);
	$.extend($.fn.treegrid.methods, methods);
	var option = {
		isRightFixed: false,
		onLoadSuccess: function(){
			var opts = $(this).datagrid('options'),boo = false;
			if(Array.isArray(opts.frozenColumns)) {
				for(var i = 0, item; item = opts.frozenColumns[i++];) {
					if(Array.isArray(item) && item.length > 0) {
						boo = true;
						break;
					}
				}
			}
			if(opts.isRightFixed && opts.frozenColumns && boo){
				$(this).datagrid('rightFixed');
			}
		},
		onResizeColumn: function(field, width){
			var opts = $(this).datagrid('options'),boo = false;
			if(Array.isArray(opts.frozenColumns)) {
				for(var i = 0, item; item = opts.frozenColumns[i++];) {
					if(Array.isArray(item) && item.length > 0) {
						boo = true;
						break;
					}
				}
			}
			if(opts.isRightFixed && opts.frozenColumns && boo){
				$(this).datagrid('rightFixed');
			}
		}
	};
	$.extend($.fn.datagrid.defaults, option);
	$.extend($.fn.treegrid.defaults, option);
})(jQuery);